package com.zzxx.controller;
import com.github.pagehelper.PageInfo;
import com.zzxx.entity.*;
import com.zzxx.exceptions.PriceException;
import com.zzxx.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("ticket")
public class TicketController {
    @Autowired
    private TicketService ticketService;
    @Autowired(required = false)
    private HttpServletRequest request;
    @RequestMapping("findOne")
    public Ticket findOne(String tid){
        Ticket ticket=ticketService.findOne(tid);
        return ticket;
    }

    @RequestMapping("findGymTicketByOid")
    public Orders findGymTicketByOid(String oid){
        Orders orders=ticketService.findGymTicketByOid(oid);
        return orders;
    }

    @RequestMapping("buy")
    public String buy(@RequestBody Orders01 order01){
        Orders orders = order01.getOrders();
        Integer num = order01.getNum();
        try {
            ticketService.buy(num,orders);
        } catch (PriceException e) {
            return e.getMessage();
        }
        return "succeed";
    }

    @RequestMapping("findByGid")
    public PageInfo<Ticket> findByGid(@RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "2") Integer pageSize, @RequestParam(defaultValue = "2") Integer navigatePages,@RequestParam(defaultValue = "2") String gid){
       PageInfo<Ticket> pageInfo=ticketService.queryByGid(start, pageSize,gid);
        pageInfo.setNavigatePages(navigatePages);
        return pageInfo;
    }

    @RequestMapping("findTicketByUid")
    public PageInfo<Orders> findTicketByUid(@RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "2") Integer pageSize, @RequestParam(defaultValue = "2") Integer navigatePages, @RequestParam(defaultValue = "2") String uid){
        PageInfo<Orders> pageInfo=ticketService.queryTicketByUid(start, pageSize,uid);
        pageInfo.setNavigatePages(navigatePages);
        return pageInfo;
    }

    @RequestMapping("tuiById")
    public int tuiById(String id){
        int row=ticketService.tuiById(id);
        return row;
    }
    @RequestMapping("deleteNum")
    public String deleteNum(String id,String num){
        ticketService.deleteNum(id,num);
        return "ok";
    }

    @RequestMapping(value = "loadManagerTicket/{gid}",method = RequestMethod.GET)
    public List<Ticket> findManagerTicket(@PathVariable("gid") String gid){
        Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
        if (loginManager==null) return null;
        List<Ticket> list = ticketService.findManagerTicket(loginManager.getId(), gid);
        return list;
    }

    @RequestMapping(value = "detail/{tid}", method = RequestMethod.GET)
    public TicketAndRule detail(@PathVariable("tid") String tid){
        Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
        if (loginManager==null) return null;
        TicketAndRule tar = ticketService.findTar(loginManager.getId(), tid);
        return tar;
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(@RequestBody Map<String,TicketAndRule> map){
//        TicketAndRule tar = JSON.parseObject(JSON.toJSONString(map.get("ticketAndRule")), TicketAndRule.class);
        TicketAndRule tar = map.get("ticketAndRule");
        if (tar==null) return "程序错误，请重新输入";
        if (tar.getRule().getStarttime().after(tar.getRule().getEndtime())) return "开始时间不能晚于结束时间";
        ticketService.updateTar(tar);
        return "success";
    }
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(@RequestBody Map<String,TicketAndRule> map){
        TicketAndRule tar = map.get("ticketAndRule");
        if (tar==null) return "程序错误，请重新输入";
        if (tar.getRule().getStarttime().after(tar.getRule().getEndtime())) return "开始时间不能晚于结束时间";
        ticketService.addTar(tar);
        return "success";
    }
    @RequestMapping(value = "delete/{tid}",method = RequestMethod.DELETE)
    public String delete(@PathVariable("tid") String tid){
        ticketService.delete(tid);
        return "success";
    }
    @RequestMapping("tui")
    public Boolean tui(String id,String time){
        Boolean flag= null;
        try {
            flag = ticketService.tui(id,time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return flag;
    }
}
